package CoreClass;

import java.util.*;

public class Business
{
	private DBCommon _dbc = new DBCommon();

	public final boolean BankLog(int hy_num, java.math.BigDecimal money, String bz)
	{
		String sql = "Insert Into [Bank_Log]([hy_num],[Balance],[bz]) values(@num,@balance,@bz)";
		SqlCommand cmd = this.getdbc().GetCommandBySql(sql);
		cmd.Parameters.Add("@num", SqlDbType.Int).Value = hy_num;
		cmd.Parameters.Add("@balance", SqlDbType.Money).Value = money;
		cmd.Parameters.Add("@bz", SqlDbType.VarChar, 0x100).Value = bz;
		try
		{
			cmd.Connection.Open();
			cmd.ExecuteNonQuery();
			cmd.Connection.Close();
			cmd.Connection.Dispose();
			cmd.Dispose();
			return true;
		}
		catch (java.lang.Exception e)
		{
			cmd.Connection.Dispose();
			cmd.Dispose();
			return false;
		}
	}

	public final boolean BankLog_tx(int hy_num, int tx_id, java.math.BigDecimal money, String bz)
	{
		String sql = "Insert Into [Bank_Log]([hy_num],[Balance],[bz],[tx_id]) values(@num,@balance,@bz,@tx_id)";
		SqlCommand cmd = this.getdbc().GetCommandBySql(sql);
		cmd.Parameters.Add("@num", SqlDbType.Int).Value = hy_num;
		cmd.Parameters.Add("@ti_id", SqlDbType.Int).Value = tx_id;
		cmd.Parameters.Add("@balance", SqlDbType.Money).Value = money;
		cmd.Parameters.Add("@bz", SqlDbType.VarChar, 0x100).Value = bz;
		try
		{
			cmd.Connection.Open();
			cmd.ExecuteNonQuery();
			cmd.Connection.Close();
			cmd.Connection.Dispose();
			cmd.Dispose();
			return true;
		}
		catch (java.lang.Exception e)
		{
			cmd.Connection.Dispose();
			cmd.Dispose();
			return false;
		}
	}

	public final boolean BankLog_updatebz(int id, int hy_num, String bz)
	{
		String sql = "update [Bank_Log] set bz=@bz where tx_id=@id and hy_num=@num ";
		SqlCommand cmd = this.getdbc().GetCommandBySql(sql);
		cmd.Parameters.Add("@num", SqlDbType.Int).Value = hy_num;
		cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
		cmd.Parameters.Add("@bz", SqlDbType.VarChar, 0x100).Value = bz;
		try
		{
			cmd.Connection.Open();
			cmd.ExecuteNonQuery();
			cmd.Connection.Close();
			cmd.Connection.Dispose();
			cmd.Dispose();
			return true;
		}
		catch (java.lang.Exception e)
		{
			cmd.Connection.Dispose();
			cmd.Dispose();
			return false;
		}
	}

	public final boolean BankOperation(int hy_num, java.math.BigDecimal money)
	{
		return this.BankOperation(hy_num, money, false);
	}

	public final boolean BankOperation(int hy_num, java.math.BigDecimal money, boolean add)
	{
		String sql = "";
		if (money.compareTo(java.math.BigDecimal.ZERO) == 0)
		{
			return true;
		}
		if (money.compareTo(java.math.BigDecimal.ZERO) < 0)
		{
			money = Math.abs(money);
			if (this.getUserBalance(hy_num).compareTo(money) < 0)
			{
				return false;
			}
			sql = "Update [Bank] Set [Output_Money]=[Output_Money]+" + money.toString() + ", [Balance]=[Balance]-" + money.toString() + " where hy_num=" + String.valueOf(hy_num) + " and zt='q'";
			return (this.getdbc().ExcuteSqlCount(sql) > 0);
		}
		if (add)
		{
			sql = "Update [Bank] Set [Input_Money]=[Input_Money]+" + money.toString() + ", [Balance]=[Balance]+" + money.toString() + " where hy_num=" + String.valueOf(hy_num);
		}
		else
		{
			sql = "Update [Bank] Set [Output_Money]=[Output_Money]-" + money.toString() + ", [Balance]=[Balance]+" + money.toString() + " where hy_num=" + String.valueOf(hy_num);
		}
		return (this.getdbc().ExcuteSqlCount(sql) > 0);
	}

	public final boolean BankOperation(int hy_num, java.math.BigDecimal money, String bz)
	{
		if (this.BankOperation(hy_num, money, false))
		{
			if (this.BankLog(hy_num, money, bz))
			{
				return true;
			}
		  //  this.BankOperation(hy_num, decimal.op_UnaryNegation(money), false);
			return false;
		}
		return false;
	}

	public final boolean BankOperation(int hy_num, java.math.BigDecimal money, boolean add, String bz)
	{
		if (this.BankOperation(hy_num, money, add))
		{
			if (this.BankLog(hy_num, money, bz))
			{
				return true;
			}
		   // this.BankOperation(hy_num, decimal.op_UnaryNegation(money), add);
			return false;
		}
		return false;
	}

	public final ArrayList BuyHmFangAn(int hy_num, String hy_name, int fid, int fenshu)
	{
		ArrayList al = new ArrayList();
		al.add(false);
		al.add("");
		if (fenshu < 1)
		{
			al.set(1, "\u5fc5\u987b\u8d2d\u4e701\u4efd\u4ee5\u4e0a\uff01");
			return al;
		}
		DataTable dtfa = this.getdbc().GetDataTableBySql("select top 1 id,CaiZhong_id,QiHao,FaPrice,FenShu,FenShu2,ExpTime,chuli from [Hm_FangAn] where [id]=" + String.valueOf(fid) + "");
		if (dtfa.Rows.size() < 1)
		{
			al.set(1, "\u8d2d\u4e70\u65b9\u6848\u4e0d\u5b58\u5728\uff01");
			return al;
		}
		int czid = (int)(dtfa.Rows[0]["CaiZhong_Id"]);
		String qh = dtfa.Rows[0]["QiHao"].toString();
		int fafenshu = (int)(dtfa.Rows[0]["FenShu"]);
		int fafenshu2 = (int)(dtfa.Rows[0]["FenShu2"]);
		if ((fafenshu - fafenshu2) < fenshu)
		{
			al.set(1, "\u65b9\u6848\u5269\u4f59\u4efd\u6570\u4e0d\u8db3\uff01");
			return al;
		}
		if ((java.time.LocalDateTime)(dtfa.Rows[0]["ExpTime"]) < java.time.LocalDateTime.now())
		{
			al.set(1, "\u65b9\u6848\u8ba4\u8d2d\u65f6\u95f4\u5df2\u8fc7\uff01");
			return al;
		}
		if ((boolean)(dtfa.Rows[0]["chuli"]))
		{
			al.set(1, "\u65b9\u6848\u5df2\u7ecf\u505c\u6b62\u8ba4\u8d2d\uff01");
			return al;
		}
		java.math.BigDecimal reqmoney = fenshu * (java.math.BigDecimal)(dtfa.Rows[0]["FaPrice"]);
	  //  if (this.BankOperation(hy_num, decimal.op_UnaryNegation(reqmoney)))
		if (true)
		{
			String sql = "Update Hm_FangAn Set FenShu2 = FenShu2 + " + String.valueOf(fenshu) + " Where [Id]=" + String.valueOf(fid);
			this.getdbc().ExcuteSql(sql);
			sql = "Insert Into Hm_Infos(FangAn_Id,Hy_Num,Hy_Name,FenShu,Price,Amt) Values(" + String.valueOf(fid) + "," + String.valueOf(hy_num) + ",'" + hy_name + "'," + String.valueOf(fenshu) + "," + dtfa.Rows[0]["FaPrice"].toString() + "," + reqmoney.toString() + ")";
			this.getdbc().ExcuteSql(sql);
		 //   this.BankLog(hy_num, decimal.op_UnaryNegation(reqmoney), "\u8ba4\u8d2d\u5408\u4e70\u65b9\u6848" + fenshu.ToString() + "\u4efd\uff0c\u65b9\u6848\u7f16\u53f7\uff1a" + fid.ToString());
			al.set(0, true);
			al.set(1, "\u6210\u529f\u8ba4\u8d2d\u5408\u4e70\u65b9\u6848\uff08\u7f16\u53f7\uff1a" + String.valueOf(fid) + "\uff09" + String.valueOf(fenshu) + "\u4efd");
			return al;
		}
		al.set(1, "\u8ba4\u8d2d\u6b3e\u6263\u9664\u5931\u8d25\uff01\u53ef\u80fd\u4f60\u7684\u5e10\u6237\u4f59\u989d\u4e0d\u591f\uff01");
		return al;
	}

	public final int calcXingY(java.math.BigDecimal money)
	{
		if (money.compareTo(100) < 0)
		{
			return 0;
		}
		if (money.compareTo(1000) < 0)
		{
			return 20;
		}
		if (money.compareTo(10000) < 0)
		{
			return 50;
		}
		if (money.compareTo(100000) < 0)
		{
			return 100;
		}
		if (money.compareTo(1000000) < 0)
		{
			return 200;
		}
		if (money.compareTo(2000000) < 0)
		{
			return 300;
		}
		if (money.compareTo(4000000) < 0)
		{
			return 400;
		}
		return 500;
	}

	public final boolean Check_DongJie(int hy_num)
	{
		String strsql = "select zt from Bank where hy_num=" + hy_num;
		DataTable Dt = null;
		Dt = this.getdbc().GetDataTableBySql(strsql);
		if ((Dt != null) && (Dt.Rows.size() > 0))
		{
			return (Dt.Rows[0][0].toString().equals("q"));
		}
		return false;
	}

	public final void checkLogin(Page curPage)
	{
		if (((curPage.Request.Cookies["hy_name"] == null) || (curPage.Request.Cookies["hy_name"].Value.equals(""))) || ((curPage.Request.Cookies["hy_num"] == null) || (curPage.Request.Cookies["hy_num"].Value.equals(""))))
		{
			curPage.Response.Write("<script language=\"javascript\">alert(\"\u60a8\u6ca1\u6709\u767b\u9646\u6216\u662f\u767b\u9646\u8d85\u65f6\u8bf7\u767b\u9646\uff01\");top.location=\"/passport/login.aspx?url=\"+top.location+\"\";</script>");
			curPage.Response.End();
		}
	}

	public final boolean DaiG_GouMai(String caizhong_id, String hy_num, String hy_name, String qihao, String number, String zhushu, String multiple, String unitprice, String totalprice)
	{
		String strsql = "insert DaiGou (CaiZhong_id,Hy_Num,Hy_Name,QiHao,Number,zhushu,Multiple,unitprice,totalprice)";
		strsql = strsql + " values (@CaiZhong_id,@Hy_Num,@Hy_Name,@QiHao,@Number,@zhushu,@Multiple,@unitprice,@totalprice)";
		SqlCommand Comm = this.getdbc().GetCommandBySql();
		Comm.Parameters.Add("@CaiZhong_id", SqlDbType.Int).Value = Integer.parseInt(caizhong_id);
		Comm.Parameters.Add("@Hy_Num", SqlDbType.Int).Value = hy_num;
		Comm.Parameters.Add("@Hy_Name", SqlDbType.VarChar, 50).Value = hy_name;
		Comm.Parameters.Add("@QiHao", SqlDbType.VarChar, 50).Value = qihao;
		Comm.Parameters.Add("@Number", SqlDbType.VarChar, 500).Value = number;
		Comm.Parameters.Add("@zhushu", SqlDbType.VarChar, 50).Value = zhushu;
		Comm.Parameters.Add("@Multiple", SqlDbType.VarChar, 10).Value = multiple;
		Comm.Parameters.Add("@unitprice", SqlDbType.Money).Value = unitprice;
		Comm.Parameters.Add("@totalprice", SqlDbType.Money).Value = totalprice;
		Comm.CommandText = strsql;
		Comm.Connection.Open();
		try
		{
			Comm.ExecuteNonQuery();
			Comm.Connection.Close();
			return true;
		}
		catch (java.lang.Exception e)
		{
			Comm.Connection.Close();
			return false;
		}
	}

	public final SqlDataReader GetQiHaoInfo(int id)
	{
		return this.getdbc().GetDataReaderBySql("select top 1 * from [QiShu] where [Id]=" + String.valueOf(id) + "");
	}

	public final SqlDataReader GetQiHaoInfo(int czid, String qh)
	{
		return this.getdbc().GetDataReaderBySql("select top 1 * from [QiShu] where [CaiZhong_Id]=" + String.valueOf(czid) + " And [QiHao]='" + qh.toString() + "'");
	}

	public final java.math.BigDecimal getUserBalance(int hy_num)
	{
		SqlDataReader dr = this.getdbc().GetDataReaderBySql("select top 1 [Balance] From [Bank] where [Hy_Num]=" + String.valueOf(hy_num) + "");
		java.math.BigDecimal returnValue = new java.math.BigDecimal(0);
		if (dr.Read())
		{
			returnValue = (java.math.BigDecimal)dr.getItem("Balance");
		}
		else
		{
			returnValue = new java.math.BigDecimal(-1);
		}
		dr.Close();
		return returnValue;
	}

	public final int getUserLevel(int scope)
	{
		if (scope < 3)
		{
			return 0;
		}
		if (scope < 10)
		{
			return 1;
		}
		if (scope < 40)
		{
			return 2;
		}
		if (scope < 90)
		{
			return 3;
		}
		if (scope < 150)
		{
			return 4;
		}
		if (scope < 250)
		{
			return 5;
		}
		if (scope < 500)
		{
			return 6;
		}
		if (scope < 0x3e8)
		{
			return 7;
		}
		if (scope < 0x7d0)
		{
			return 8;
		}
		if (scope < 0x1388)
		{
			return 9;
		}
		if (scope < 0x2710)
		{
			return 10;
		}
		if (scope < 0x4e20)
		{
			return 11;
		}
		if (scope < 0xc350)
		{
			return 12;
		}
		if (scope < 0x186a0)
		{
			return 13;
		}
		if (scope < 0x30d40)
		{
			return 14;
		}
		return 15;
	}

	public final int getUserLevel(String hy_name)
	{
		int rtn = 0;
		SqlDataReader dr = this.getdbc().GetDataReaderBySql("select top 1 Xingyd From User_Extent Where Hy_Name='" + hy_name + "'");
		if (dr.Read())
		{
			rtn = this.getUserLevel(Integer.parseInt(dr.getItem("Xingyd").toString()));
		}
		dr.Close();
		return rtn;
	}

	public final boolean isLogin(Page curPage)
	{
		if ((curPage.Request.Cookies["hy_name"] == null) || (curPage.Request.Cookies["hy_name"].Value.equals("")))
		{
			return false;
		}
		return true;
	}

	public final boolean UpdateXingYong(int hy_num, int mark)
	{
		return this.UpdateXingYong(String.valueOf(hy_num), mark);
	}

	public final boolean UpdateXingYong(String Hy_Num, int mark)
	{
		if ((Hy_Num == null) || (Hy_Num.equals("")))
		{
			return false;
		}
		if (mark != 0)
		{
			try
			{
				return (this.getdbc().ExcuteSqlCount("Update User_Extent Set Xingyd = Xingyd+(" + String.valueOf(mark) + ") where Hy_Num=" + Hy_Num + "") > 0);
			}
			catch (java.lang.Exception e)
			{
				return false;
			}
		}
		return true;
	}

	public final DBCommon getdbc()
	{
		return this._dbc;
	}
}